package com.hhsj.dynamicui.server.servlet.admin.module;

import com.hhsj.androidserver.HttpServlet;
import com.hhsj.androidserver.HttpServletRequest;
import com.hhsj.androidserver.HttpServletResponse;
import com.hhsj.androidserver.WebServlet;
import com.hhsj.dynamicui.model.base.BaseModel;
import com.hhsj.dynamicui.server.DynamicUIService;

import java.net.URLDecoder;
import java.sql.PreparedStatement;

/**
 * 创建一个页面
 */
@WebServlet("/uhomecp-admin/page_get")
public class GetPageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse rep) throws Exception {
        doPost(req, rep);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse rep) throws Exception {
        //1.解析
        String const_type = URLDecoder.decode(req.getParameterValues("const_type")[0], "utf-8").toLowerCase();
        String const_code = URLDecoder.decode(req.getParameterValues("const_code")[0], "utf-8").toLowerCase();
        String const_name = URLDecoder.decode(req.getParameterValues("const_name")[0], "utf-8").toLowerCase();
        String const_desc = URLDecoder.decode(req.getParameterValues("desc")[0], "utf-8").toLowerCase();

        System.out.println("const_type =" + const_type);
        System.out.println("const_code =" + const_code);
        System.out.println("const_name =" + const_name);
        System.out.println("const_desc =" + const_desc);

        //2.创建数据模型并保持到数据库

        //JDBC 远程处理数据到 MySql 中
        String sql = String.format("INSERT INTO const(const_type,const_code,const_name,const_desc) VALUES (\'%s\',\'%s\',\'%s\',\'%s\');",
                const_type, const_code, const_name, const_desc);

        System.out.println(sql);

        PreparedStatement statement = DynamicUIService.getInstance().getConnection().prepareStatement(sql);
        statement.execute();

        //3.返回响应结果
        rep.print(BaseModel.newEmpty("0", "成功").toJSONString());
    }
}
